{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Predict With Our Amazon Comprehend Custom Classifier Model"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"img/comprehend.png\" width=\"80%\" align=\"left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Note that Amazon Comprehend is currently only supported in a subset of regions: \n",
    "\n",
    "* US East (N. Virginia), US East (Ohio), US West (Oregon)\n",
    "* Canada (Central)\n",
    "* Europe (London), Europe (Ireland), Europe (Frankfurt)\n",
    "* Asia Pacific (Mumbai), Asia Pacific (Seoul), Asia Pacific (Tokyo), Asia Pacific (Singapore), Asia Pacific (Sydney)\n",
    "\n",
    "You can check https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/ for details and updates. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import boto3\n",
    "import sagemaker\n",
    "import pandas as pd\n",
    "\n",
    "sess = sagemaker.Session()\n",
    "bucket = sess.default_bucket()\n",
    "role = sagemaker.get_execution_role()\n",
    "region = boto3.Session().region_name\n",
    "\n",
    "from botocore.config import Config\n",
    "\n",
    "config = Config(retries={\"max_attempts\": 10, \"mode\": \"adaptive\"})\n",
    "\n",
    "comprehend = boto3.Session().client(service_name=\"comprehend\", region_name=region)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%store -r comprehend_training_job_arn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "try:\n",
    "    comprehend_training_job_arn\n",
    "except NameError:\n",
    "    print(\"***************************************************************************\")\n",
    "    print(\"[ERROR] PLEASE WAIT FOR THE PREVIOUS NOTEBOOK TO FINISH *******************\")\n",
    "    print(\"[ERROR] OR THIS NOTEBOOK WILL NOT RUN PROPERLY ****************************\")\n",
    "    print(\"***************************************************************************\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "print(comprehend_training_job_arn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%store -r comprehend_endpoint_arn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "try:\n",
    "    comprehend_endpoint_arn\n",
    "except NameError:\n",
    "    print(\"***************************************************************************\")\n",
    "    print(\"[ERROR] PLEASE WAIT FOR THE PREVIOUS NOTEBOOK TO FINISH *******************\")\n",
    "    print(\"[ERROR] OR THIS NOTEBOOK WILL NOT RUN PROPERLY ****************************\")\n",
    "    print(\"***************************************************************************\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "print(comprehend_endpoint_arn)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Deploy Endpoint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "describe_response = comprehend.describe_endpoint(EndpointArn=comprehend_endpoint_arn)\n",
    "print(describe_response)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Check Endpoint Status"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.core.display import display, HTML\n",
    "\n",
    "display(\n",
    "    HTML(\n",
    "        '<b>Review <a target=\"blank\" href=\"https://console.aws.amazon.com/comprehend/v2/home?region={}#classifier-details/{}/endpoints/{}/details\">Comprehend Model Endpoint</a></b>'.format(\n",
    "            region, comprehend_training_job_arn, comprehend_endpoint_arn\n",
    "        )\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "\n",
    "max_time = time.time() + 3 * 60 * 60  # 3 hours\n",
    "while time.time() < max_time:\n",
    "    describe_response = comprehend.describe_endpoint(EndpointArn=comprehend_endpoint_arn)\n",
    "    status = describe_response[\"EndpointProperties\"][\"Status\"]\n",
    "    print(\"Endpoint: {}\".format(status))\n",
    "\n",
    "    if status == \"IN_SERVICE\" or status == \"IN_ERROR\":\n",
    "        break\n",
    "\n",
    "    time.sleep(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [INFO] _Feel free to continue to the next workshop section while this notebook is running._"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Predict with Endpoint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "txt = \"\"\"I loved it!  I will recommend this to everyone.\"\"\"\n",
    "\n",
    "response = comprehend.classify_document(Text=txt, EndpointArn=comprehend_endpoint_arn)\n",
    "\n",
    "import json\n",
    "\n",
    "print(json.dumps(response, indent=2, default=str))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "txt = \"\"\"It's OK.\"\"\"\n",
    "\n",
    "response = comprehend.classify_document(Text=txt, EndpointArn=comprehend_endpoint_arn)\n",
    "\n",
    "import json\n",
    "\n",
    "print(json.dumps(response, indent=2, default=str))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "txt = \"\"\"Really bad.  I hope they don't make this anymore.\"\"\"\n",
    "\n",
    "response = comprehend.classify_document(Text=txt, EndpointArn=comprehend_endpoint_arn)\n",
    "\n",
    "import json\n",
    "\n",
    "print(json.dumps(response, indent=2, default=str))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Release Resources"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%html\n",
    "\n",
    "<p><b>Shutting down your kernel for this notebook to release resources.</b></p>\n",
    "<button class=\"sm-command-button\" data-commandlinker-command=\"kernelmenu:shutdown\" style=\"display:none;\">Shutdown Kernel</button>\n",
    "        \n",
    "<script>\n",
    "try {\n",
    "    els = document.getElementsByClassName(\"sm-command-button\");\n",
    "    els[0].click();\n",
    "}\n",
    "catch(err) {\n",
    "    // NoOp\n",
    "}    \n",
    "</script>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%javascript\n",
    "\n",
    "try {\n",
    "    Jupyter.notebook.save_checkpoint();\n",
    "    Jupyter.notebook.session.delete();\n",
    "}\n",
    "catch(err) {\n",
    "    // NoOp\n",
    "}"
   ]
  }
 ],
 "metadata": {
  "instance_type": "ml.t3.medium",
  "kernelspec": {
   "display_name": "Python 3 (Data Science)",
   "language": "python",
   "name": "python3__SAGEMAKER_INTERNAL__arn:aws:sagemaker:us-east-1:081325390199:image/datascience-1.0"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
